package com.hundsun.cprs.yyt.service.goods.service.mq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.hundsun.cprs.yyt.common.enums.EnumAreaCode;
import com.hundsun.cprs.yyt.common.exception.ServiceException;
import com.hundsun.cprs.yyt.service.goods.dto.GoodsAreaInfo;
import com.hundsun.cprs.yyt.service.goods.dto.GoodsInfo;
import com.hundsun.cprs.yyt.service.goods.service.GoodsAreaSerivce;
import com.hundsun.cprs.yyt.service.goods.service.GoodsService;
import com.hundsun.cprs.yyt.service.goods.service.GoodsSkuService;
import com.hundsun.cprs.yyt.service.rabbit.RoutingKeyConstants;
import com.hundsun.cprs.yyt.service.trade.domain.TradeEntrust;

/**
 * 订阅委售模块的委售单的上报及撤销事件，变更零售区的商品库存
 * 
 * @author linrl
 * @date 2019年3月21日
 */
@Component
public class GoodsStockListener implements MessageListener {

    @Autowired
    private GoodsSkuService skuService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsAreaSerivce goodsAreaSerivce;

    @Override
    public void onMessage(Message message) {
        String receviedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();

        // 委售单上报通知
        if (receviedRoutingKey.equals(RoutingKeyConstants.YYT_TRADE_ENTRUST_COMMIT_SUCCESS)) {
            TradeEntrust tradeEntrust = JSON.parseObject(new String(message.getBody()), TradeEntrust.class);
            if (tradeEntrust.isSell()) {
                // 零售区商品入库
                GoodsInfo goods = getAndPutOnGoodsInfo(tradeEntrust.getGoodsId(),EnumAreaCode.RETAIL_AREA.getCode());
                skuService.addGoodsStock(goods.getId().longValue(), EnumAreaCode.RETAIL_AREA.getCode(),
                    tradeEntrust.getSurplusEntrustAmount().intValue());
            }
        }

        // 委售单撤销通知
        if (receviedRoutingKey.equals(RoutingKeyConstants.YYT_ENTRUST_STATUS_CANCELED)
            || receviedRoutingKey.equals(RoutingKeyConstants.YYT_ENTRUST_STATUS_PART_CANCELED)
            || receviedRoutingKey.equals(RoutingKeyConstants.YYT_TRADE_ENTRUST_ROLLBACK_SUCCESS)) {
            TradeEntrust tradeEntrust = JSON.parseObject(new String(message.getBody()), TradeEntrust.class);
            if (tradeEntrust.isSell()) {
                // 零售区商品出库
                skuService.reduceGoodsStock(tradeEntrust.getGoodsId(), EnumAreaCode.RETAIL_AREA.getCode(),
                    tradeEntrust.getSurplusEntrustAmount().intValue());
            }
        }
    }
    
    
    /**
     * 获取零售专区商品，无则自动上架
     */
    protected GoodsInfo getAndPutOnGoodsInfo(Long goodsId,String areaCode){
        GoodsInfo goods = goodsService.selectGoodsInfoById(goodsId.intValue(),EnumAreaCode.RETAIL_AREA.getCode());
        if (goods == null) {
            goods = goodsService.selectGoodsInfoById(goodsId.intValue());
            try {
                GoodsAreaInfo goodsAreaInfo = new GoodsAreaInfo();
                goodsAreaInfo.setAreaCode(EnumAreaCode.RETAIL_AREA.getCode());
                goodsAreaInfo.setGoods(goods);
                goodsAreaSerivce.putOnGoods(goodsAreaInfo);
            } catch (ServiceException e) {
                // 重复上架，则重新获取一次
                goods = goodsService.selectGoodsInfoById(goodsId.intValue(),EnumAreaCode.RETAIL_AREA.getCode());
            }
        }
        return goods;
    }

}
